//
//  ZhifuView.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/16.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ZhifuView.h"
#import "JTCSM.h"

static CGFloat viewheight = 1.4615;

static CGFloat top  = 0.1521;
static CGFloat labelHeight = 20;
static CGFloat space = 40;
static ZhifuView *single = nil;


@interface ZhifuView ()<UIAlertViewDelegate>
{
    UILabel *moneyLabel;
    UIImage *tempImage;
    BOOL ishaveImage;
    UIImageView *iamgev;

}

@end

@implementation ZhifuView

+ (ZhifuView *)shareSingle
{
    @synchronized(self){
        if(single == nil){
            
            single =[ [ZhifuView alloc] init];
        }
        
    }
    return single;
}
- (id)init{
   self = [super init];
    if(self){
        self.frame = CGRectMake(0, 0, KSCREENWidth, KSCREENHeight);
        self.backgroundColor = RGBACOLOR(89, 89, 89, 0.5);
        
        CGFloat backViewWidth = KSCREENWidth-2*space;
        CGFloat backViewHeight = backViewWidth*viewheight;
        UIImageView *backImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, backViewWidth, backViewHeight)];
        backImageV.image = [UIImage imageNamed:@"shoukuan.png"];
        backImageV.center = self.center;
        backImageV.userInteractionEnabled = YES;
        [self addSubview:backImageV];
        
        moneyLabel = [UILabel initWithFrame:CGRectMake(0, backViewHeight*top, backViewWidth, labelHeight) text:@"" font:22 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(100, 100, 100, 1) backgroundColor:nil];
        moneyLabel.userInteractionEnabled = YES;
        [backImageV addSubview:moneyLabel];
        CGFloat imagevSize = backViewWidth-2*space;
        iamgev= [[UIImageView alloc]initWithFrame:CGRectMake(space, KVIEW_Y(moneyLabel)+0, imagevSize,imagevSize)];
//        iamgev.image = [UIImage imageNamed:@"shoukuan.png"];
//        iamgev.center = CGPointMake(backViewWidth/2,(backViewHeight*(1-top-bottom)));
        iamgev.userInteractionEnabled = YES;
        [backImageV addSubview:iamgev];
        
        //初始化一个长按手势
        UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressView)];
        
        //长按等待时间
        longPressGest.minimumPressDuration = 2;
        
        //长按时候,手指头可以移动的距离
        longPressGest.allowableMovement = 30;
        [self addGestureRecognizer:longPressGest];
        
    }
    
    return self;
}
- (void)longPressView{
    if (ishaveImage) {
        ishaveImage = NO;
        [self loadImageFinished:tempImage];
    }
}

- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if (error) {
        ishaveImage = YES;
        [df showAlertWithTitle:@"保存失败！" message:@"请重新保存 "btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:0];
    }else{
        ishaveImage = NO;
        [df showAlertWithTitle:@"保存成功！" message:@"请打开本地相册进行扫码支付"btn1:@"已支付" btn2:@"未支付" btn3:nil delegate:self tag:123];
    }
}


- (void)setMoney:(NSString *)money imagename:(NSString *)imagename saveIamge:(NSString *)saveimagename{
    [[JTCSMSingle shareSingle].theWindow addSubview:self];
    moneyLabel.text = [NSString stringWithFormat:@"付款金额¥%@",money];
    [iamgev sd_setImageWithURL:[NSURL URLWithString:imagename]];
    ishaveImage = NO;
    [SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:saveimagename] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (!error) {
            tempImage = image;
            ishaveImage  = YES;

        }
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    [self.delegate zhifuResultWithType:(zhifuresultType)buttonIndex];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
